package com.gin.no_gay_alliance.adpter;

import android.content.Context;
import android.content.DialogInterface;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.constraintlayout.widget.ConstraintLayout;

import com.gin.nga.enums.FavorFolderType;
import com.gin.nga.method.NukeApi;
import com.gin.nga.params.nuke.favor.FavorFolderModifyParam;
import com.gin.nga.response.body.BaseMessageBody;
import com.gin.nga.response.field.FavorFolder;
import com.gin.no_gay_alliance.adpter.base.BaseBindingRecyclerViewAdapter;
import com.gin.no_gay_alliance.app.MyApplication;
import com.gin.no_gay_alliance.callback.CommonCallback;
import com.gin.no_gay_alliance.databinding.DialogFavorFolderModifyBinding;
import com.gin.no_gay_alliance.databinding.ItemFavorFolderBinding;
import com.gin.no_gay_alliance.enums.MessageWhat;
import com.gin.no_gay_alliance.utils.AlertDialogUtils;
import com.gin.no_gay_alliance.utils.InputMethodManagerUtils;

import org.apache.commons.lang3.ObjectUtils;

import java.util.List;
import java.util.Locale;

/**
 * 收藏夹适配器
 *
 * @author : ginstone
 * @version : v1.0.0
 * @since : 2023/9/5 20:09
 **/
public class FavorFolderAdapter extends BaseBindingRecyclerViewAdapter<ItemFavorFolderBinding, FavorFolder> implements View.OnClickListener,
        View.OnLongClickListener {
    /**
     * 是否允许修改
     */
    private boolean editable;

    public FavorFolderAdapter(Context context) {
        super(context);
    }

    public FavorFolderAdapter(@NonNull Context context, @NonNull List<FavorFolder> data) {
        super(context, data);
    }

    @Override
    protected void onBindViewHolder(ViewHolder<ItemFavorFolderBinding> holder, @NonNull ItemFavorFolderBinding b, int position, FavorFolder item) {
        b.tvFolderName.setText(item.getName());
        b.tvCount.setText(String.format(Locale.CHINA, "(%d)", item.getLength()));
        b.tvType.setText(item.getType() != null ? item.getType().value : null);
        // 点击事件
        b.getRoot().setTag(item);
        b.getRoot().setOnClickListener(this);
        if (editable) {
            b.getRoot().setOnLongClickListener(this);
        }
    }

    @NonNull
    @Override
    protected ItemFavorFolderBinding buildBinding(LayoutInflater inflater) {
        return ItemFavorFolderBinding.inflate(inflater);
    }

    @Override
    protected boolean useZebraStripe() {
        return false;
    }

    @Override
    protected ViewGroup.LayoutParams buildLayoutParamsForRoot() {
        return new ConstraintLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
    }

    @Override
    public void onClick(View v) {
        // 点击切换收藏夹
        FavorFolder item = (FavorFolder) v.getTag();
        handler.sendMessage(MessageWhat.OPEN_FAVOR_FOLDER, item);
    }

    @Override
    public boolean onLongClick(View v) {
        FavorFolder item = (FavorFolder) v.getTag();
        // 弹窗修改

        DialogFavorFolderModifyBinding b = DialogFavorFolderModifyBinding.inflate(LayoutInflater.from(context));

        AlertDialog dialog = AlertDialogUtils.showConfirm(context, "修改收藏夹", null, b.getRoot(), (d, which) -> {
            if (which == DialogInterface.BUTTON_POSITIVE) {
                FavorFolderModifyParam param = new FavorFolderModifyParam(b.etName.getText().toString(), b.swPublic.isChecked(), item.getId());
                NukeApi.favorFolderModify(MyApplication.getClient(), param).async(new CommonCallback<BaseMessageBody>(handler) {
                    @Override
                    public void onSuccess(BaseMessageBody body) {
                        handler.toast(body.getMessage());
                        handler.sendMessage(MessageWhat.ON_SUCCESS_FAVOR_FOLDER_EDIT, item);
                    }
                });
            }
        });

        b.etName.setText(item.getName());
        b.swPublic.setChecked(item.getType() == FavorFolderType.PUBLIC);
        b.etName.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                dialog.getButton(DialogInterface.BUTTON_POSITIVE).setEnabled(!ObjectUtils.isEmpty(s));
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });

        b.etName.requestFocus();
        b.etName.setSelection(0, b.etName.getText().length());
        InputMethodManagerUtils.show(handler, b.etName);

        return false;
    }

    public void setEditable(boolean editable) {
        this.editable = editable;
    }
}
